package com.raychi.demo.action;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.raychi.demo.entity.Student;
import com.raychi.demo.entity.Teacher;
import com.raychi.demo.service.StudentService;
import com.raychi.demo.service.TeacherService;

public class TeacherAction extends ActionSupport{
	private static final long serialVersionUID = 2697521754705499509L;
	private Teacher teacher;
	private TeacherService teacherService;
	private List<Student> list;
	private StudentService studentService;
	private Student student;
	
	/**
	 * 列出所有学生的信息
	 * @return 显示学生信息界面
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public String listSt() throws ClassNotFoundException, SQLException{
		list = studentService.listStudents();
		return "listSt";
	}
	
	/**
	 * 将学生的信息删除
	 * @return 删除完成后回到所有学生的信息显示界面
	 * @throws Exception 
	 */
	public String deleteSt() throws Exception{
		studentService.delete(student.getId());
		return listSt();
		
	}
	
	/**
	 * 修改学生信息的界面，默认显示被修改前的学生信息
	 * @return 修改学生信息的界面
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public String editSt() throws ClassNotFoundException, SQLException{
		student = studentService.getStudentById(student.getId());
		return "editSt";
	}
	
	/**
	 * 将修改完成后的学生信息存入数据库
	 * @return 返回到显示所有学生信息的界面
	 * @throws Exception 
	 */
	public String updateSt() throws Exception{
		studentService.update(student);
		return listSt();
	}
	
	/**
	 * 教师登录的界面
	 * @return
	 */
	public String login(){
		
		return "login";
	}
	
	/**
	 * 教师登出  将Session中的信息清除
	 * @return 返回到登录界面
	 */
	public String listOut(){
//		SessionUtil.outTeacherSession();
		HttpSession session = ServletActionContext.getRequest().getSession(false);
		if(session != null){
			session.invalidate();
		}
		return login();
	}
	
	/**
	 * 教师登入验证，成功则重定向到教师选择界面
	 * 失败则回到登录界面
	 */
	public String input() throws ClassNotFoundException, SQLException{
		boolean rsl = teacherService.login(teacher.getUsername(), teacher.getPassword());

		if (rsl) {
			return SUCCESS;
		}
		super.addActionError("用户名或密码错误！");
		return "login";
	}
	
	/**
	 * 教师注册界面
	 * @return
	 */
	public String regist(){
		return "regist";
	}
	
	/**
	 * 通过各种验证 将注册成功的老师信息存入数据库 
	 * @return 注册成功就返回登录界面 注册失败就返回注册界面
	 * @throws Exception
	 * @throws SQLException
	 */
	public String save() throws Exception, SQLException{
		if (teacher.getUsername() == null || teacher.getUsername().length() == 0) {
			super.addActionError("请输入用户名");
		}
		if (StringUtils.isEmpty(teacher.getPassword()) || StringUtils.isEmpty(teacher.getPassword2())) {
			super.addActionError("请输入密码");
		}
		if(!teacher.getPassword().equals(teacher.getPassword2())){
			super.addActionError("2次密码输入不一致");
		}
		
		if (super.hasActionErrors()) {
			return "regist";
		}
		
		String rsl = teacherService.save(teacher);
		if ("fail".equals(rsl)) {
			super.addActionError("用户名已被注册");
			return "regist";
		}
		super.addActionMessage("注册成功");
		return "login";
	}
	
	/**
	 * 教师选择界面
	 * @return
	 */
	public String cho(){
		return "cho";
	}

	public Teacher getTeacher() {
		return teacher;
	}

	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}

	public void setTeacherService(TeacherService teacherService) {
		this.teacherService = teacherService;
	}

	public List<Student> getList() {
		return list;
	}

	public void setList(List<Student> list) {
		this.list = list;
	}

	public StudentService getStudentService() {
		return studentService;
	}

	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}

	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}


}
